package com.leetcode.partition4;

import java.io.*;

/**
 * @author `RKC`
 * @date 2022/3/13 21:22
 */
public class LC393UTF8编码验证 {

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        int n = Integer.parseInt(reader.readLine());
        String[] ss = reader.readLine().split(" ");
        int[] data = new int[n];
        for (int i = 0; i < n; i++) data[i] = Integer.parseInt(ss[i]);
        writer.write(validUtf8(data) + "\n");
        writer.flush();
    }

    public static boolean validUtf8(int[] data) {
        int n = data.length;
        for (int i = 0; i < n; ) {
            int x = data[i], j = 7;
            //找出当前高位开始的多少个连续1
            while (j >= 0 && (x >> j & 1) == 1) j--;
            int cnt = 7 - j;
            // cnt为1或者大于4均违反编码规则
            if (cnt == 1 || cnt > 4) return false;
            //位置i后面不足cnt-1也返回false；
            if (i + cnt - 1 >= n) return false;
            for (int k = i + 1; k < i + cnt; k++) {
                if ((data[k] >> 7 & 1) == 1 && (data[k] >> 6 & 1) == 0) continue;
                return false;
            }
            if (cnt == 0) i++;
            else i += cnt;
        }
        return true;
    }
}
